import http from '@/utils/http'
export function checkUpdater(callback) {
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
		http.get('app-version').then(({data})=>{
			console.log(data);
			let _data = data.data;
			
			let _newVersion = _data.current_version;//服务端版本
			let _localVersion = widgetInfo.version;//本地版本
			let _wgtUrl = _data.update_source_url;//wgt资源升级文件
			let apkUrl = _data.apk_url;//apk文件
			let ipaUrl = _data.ipa_url;//ipa
			let force_update = _data.force_update;//是否强制更新
			let description = _data.description;
			let ready = false;
			var params = {};
			
			if(_newVersion && _localVersion){
				let	localCode = _localVersion.split(".").join('');
				let newCode = _newVersion.split(".").join('');
				if(newCode>localCode){//服务器|本地版本对比
					// 判断系统类型
					if (plus.os.name.toLowerCase() === 'android') {
						if (_wgtUrl && _wgtUrl.match(RegExp(/.wgt/))) {
							ready = true
						}else if(apkUrl){
							ready = true
						}else{}
						
					}else{
						if (_wgtUrl && _wgtUrl.match(RegExp(/.wgt/))) {
							ready = true
						}else if(ipaUrl){
							ready = true
						}else{}
					}
					if(ready){
						params._newVersion = _newVersion;
						params._localVersion = _localVersion;
						params._wgtUrl = _wgtUrl;
						params.apkUrl = apkUrl;
						params.ipaUrl = ipaUrl;
						params.force_update = force_update;
						params.ready = ready;
						params.description = description;
						callback(params);
					}
					
				}else{
					params.ready = ready;
					callback(params);
				}
			}else{
				params.ready = ready;
				callback(params);
			}
		}).catch(error => {
			console.log(error)
		});
	})
	
}